from rest_framework import serializers
from .models import *

class ExaminationQuestionsSerializer(serializers.ModelSerializer):
    # 自定义字段，用于显示枚举类型的名称
    question_type_display = serializers.CharField(source='get_question_type_display', read_only=True)
    question_class_display = serializers.CharField(source='get_question_class_display', read_only=True)
    # 自定义字段，用于显示外键关联的名称
    question_source_display = serializers.CharField(source='question_source.name', read_only=True)
    class Meta:
        model = ExaminationQuestions
        fields = '__all__'



class UserAnswerRecordAddSerializer(serializers.ModelSerializer):
    question = serializers.IntegerField(required=True)
    answer = serializers.CharField(required=True)
    class Meta:
        model = UserAnswerRecord
        fields = ['question', 'answer']

class UserAnswersRecordAddSerializer(serializers.ListSerializer):
    child = UserAnswerRecordAddSerializer()

class UserAnswerRecordSerializer(serializers.ModelSerializer):
    user_answer = serializers.CharField(source='answer')
    answer_record_id = serializers.CharField(source='id')
    id = serializers.ReadOnlyField(source='question.id')
    question = serializers.ReadOnlyField(source='question.question')
    answer = serializers.ReadOnlyField(source='question.answer')
    # 用户的答案
    options = serializers.ReadOnlyField(source='question.options')
    explanation = serializers.ReadOnlyField(source='question.explanation')
    question_type_display = serializers.ReadOnlyField(source='question.get_question_type_display') # 显示具体的枚举类型
    question_class_display = serializers.ReadOnlyField(source='question.get_question_class_display') # 显示具体的枚举类型
    question_source_display = serializers.ReadOnlyField(source='question.question_source.name')
    question_source = serializers.ReadOnlyField(source='question.question_source.id')
    question_type = serializers.ReadOnlyField(source='question.question_type')
    question_class = serializers.ReadOnlyField(source='question.question_class')
    
    class Meta:
        model = UserAnswerRecord
        fields = [
            'user_answer', 
            'answer_record_id', 
            'id', 
            'question', 
            'answer', 
            'options',
            'explanation',
            'question_source', 
            'question_type',
            'question_class',
            'question_type_display',
            'question_class_display',
            'question_source_display',
            'create_at']

